//
//  PhotoViewController.m
//  卢卡的时光网
//
//  Created by 卡神 on 15/7/22.
//  Copyright © 2015年 lok. All rights reserved.
//

#import "PhotoViewController.h"
#import "common.h"
#import "UIImageView+WebCache.h"
#import "News.h"
#import "DataService.h"


@interface PhotoViewController () <UIScrollViewDelegate>
{
    NSMutableArray *_data;
    UIScrollView *_mainScrollView;
    UIImageView *_left;
    UIImageView *_rigth;
    UIImageView *_centerView;
}


@end

@implementation PhotoViewController


- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self _loadData];
    [self _createMainScrollView];
    [self _creatImageView];
    
    // Do any additional setup after loading the view.
}

- (void)setK:(int)k {
    _k = k;
    [self _reloadImage];
}

- (void)_loadData {
    _data = [[NSMutableArray alloc] init];
    NSArray *array = [DataService readJsonFlieWithNamed:detailJson];
    for (NSDictionary *dic in array) {
        News *new = [[News alloc] init];
        new.image = [dic objectForKey:@"image"];
        [_data addObject:new];
    }
    
}

- (void)_createMainScrollView {
    _mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _mainScrollView.contentSize = CGSizeMake(kWidth * 3, kHeight);
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.delegate = self;
    [_mainScrollView setContentOffset:CGPointMake(kWidth, 0) animated:NO];
    [self.view addSubview:_mainScrollView];
    
}

- (void)_creatImageView {
    UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    view.contentSize = self.view.bounds.size;
    UIScrollView *view1 = [[UIScrollView alloc] initWithFrame:CGRectMake(kWidth, 0, kWidth, kHeight)];
    view.contentSize = self.view.bounds.size;
    view1.delegate = self;
    view1.maximumZoomScale = 2;
    UIScrollView *view3 = [[UIScrollView alloc] initWithFrame:CGRectMake(kWidth * 2, 0, kWidth, kHeight)];
    view.contentSize = self.view.bounds.size;
    _left = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, kWidth, 200)];
    _rigth =  [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, kWidth, 200)];
    _centerView =  [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, kWidth, 200)];
    
    [view addSubview:_left];
    [view1 addSubview:_centerView];
    [view3 addSubview:_rigth];
    [_mainScrollView addSubview:view];
    [_mainScrollView addSubview:view1];
    [_mainScrollView addSubview:view3];
}

- (void)setImage {
    [self _reloadImage];
}

- (void)scrollViewDidEndDecelerating:(nonnull UIScrollView *)scrollView {
    [self reloadImage];
    _mainScrollView.contentOffset = CGPointMake(kWidth, 0);
}

- (nullable UIView *)viewForZoomingInScrollView:(nonnull UIScrollView *)scrollView {
    return _centerView;
}

- (void)reloadImage {
    CGPoint origin = [_mainScrollView contentOffset];
    if (origin.x > kWidth) {
        self.k++;
        self.k = (int)(self.k + [_data count]) % [_data count];
        
    } else if (origin.x < kWidth) {
        self.k--;
        self.k = (int)(self.k + [_data count]) % [_data count];
    }
    [self _reloadImage];
   
}

- (void)_reloadImage {
    int left1, right1;
    left1 = (int)((self.k  + [_data count] - 1) % [_data count]);
    right1 = (int)((self.k + 1 +  [_data count]) % [_data count]);
     
    [_left sd_setImageWithURL:[NSURL URLWithString:((News *)_data[left1]).image] placeholderImage:[UIImage imageNamed:@"1"]];
  
    
    [_centerView sd_setImageWithURL:[NSURL URLWithString:((News *)_data[self.k]).image] placeholderImage:[UIImage imageNamed:@"1"]];
    [_rigth sd_setImageWithURL:[NSURL URLWithString:((News *)_data[right1]).image] placeholderImage:[UIImage imageNamed:@"1"]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
